package com.chuantou.yuba.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.ImageTO;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.ViewHolder;
import com.viewpagerindicator.IconPagerAdapter;

public class ImageAdapter extends PagerAdapter implements IconPagerAdapter {
	private Context mContext;

	public ImageAdapter(Context context) {
		this.mContext = context;
	}

	private List<ImageTO> images = new ArrayList<ImageTO>();

	public void addImage(List<ImageTO> list) {
		if (list != null && !list.isEmpty()) {
			images.addAll(list);
		}
	}

	public void clear() {
		images.clear();
	}

	public ImageTO getItemAtPosition(int position) {
		return images.get(position);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageTO image = images.get(position);
		View contentView = View.inflate(mContext, R.layout.image_viewpager_item, null);
		NetworkImageView coverView = ViewHolder.get(contentView, R.id.image);
		String imageUrl = image.getUrl();
		BitmapBinder.bindNetImage(mContext, coverView, imageUrl, R.drawable.default_image_big, false);
		container.addView(contentView);
		return contentView;
	}

	@Override
	public int getIconResId(int index) {
		return R.drawable.dot_selector;
	}

	@Override
	public int getCount() {
		return images.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(ViewGroup collection, int position, Object view) {
		collection.removeView((View) view);
	}

}